from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app import crud, schemas
from app.database import get_db
from app.api.auth import get_current_user
from app.schemas.user import UserInDB

router = APIRouter()

# 创建问题
@router.post("/", response_model=schemas.IssueResponse, status_code=status.HTTP_201_CREATED)
def create_issue(
    issue: schemas.IssueCreate,
    db: Session = Depends(get_db),
    current_user: UserInDB = Depends(get_current_user)
):
    created_issue = crud.issue.create_issue(db=db, issue=issue, submitter_id=current_user.id)
    return created_issue

# 获取问题列表
@router.get("/", response_model=list[schemas.IssueResponse])
def read_issues(
    skip: int = 0,
    limit: int = 100,
    status: str = None,
    category: str = None,
    priority: str = None,
    db: Session = Depends(get_db)
):
    issues = crud.issue.get_issues(
        db, 
        skip=skip, 
        limit=limit, 
        status=status, 
        category=category, 
        priority=priority
    )
    return issues

# 获取问题详情
@router.get("/{issue_id}", response_model=schemas.IssueResponse)
def read_issue(issue_id: int, db: Session = Depends(get_db)):
    db_issue = crud.issue.get_issue(db, issue_id=issue_id)
    if db_issue is None:
        raise HTTPException(status_code=404, detail="Issue not found")
    
    # 获取附件信息
    attachments = crud.attachment.get_attachments_by_entity(db, "issue", issue_id)
    
    # 转换为响应格式
    issue_dict = {
        "id": db_issue.id,
        "title": db_issue.title,
        "description": db_issue.description,
        "type": db_issue.type,
        "category": db_issue.category,
        "priority": db_issue.priority,
        "status": db_issue.status,
        "submitter_id": db_issue.submitter_id,
        "assignee_id": db_issue.assignee_id,
        "similarity_score": db_issue.similarity_score,
        "matched_case_id": db_issue.matched_case_id,
        "created_at": db_issue.created_at,
        "updated_at": db_issue.updated_at,
        "closed_at": db_issue.closed_at,
        "attachments": [
            {
                "id": att.id,
                "filename": att.filename,
                "original_filename": att.original_filename,
                "file_size": att.file_size,
                "mime_type": att.mime_type,
                "file_url": f"/api/attachments/download/{att.id}",
                "uploaded_by": att.uploaded_by,
                "created_at": att.created_at
            } for att in attachments
        ]
    }
    
    return issue_dict

# 更新问题
@router.put("/{issue_id}", response_model=schemas.IssueResponse)
def update_issue(
    issue_id: int,
    issue: schemas.IssueUpdate,
    db: Session = Depends(get_db),
    current_user: UserInDB = Depends(get_current_user)
):
    db_issue = crud.issue.update_issue(db, issue_id=issue_id, issue=issue)
    if db_issue is None:
        raise HTTPException(status_code=404, detail="Issue not found")
    return db_issue

# 删除问题
@router.delete("/{issue_id}", response_model=schemas.IssueResponse)
def delete_issue(
    issue_id: int,
    db: Session = Depends(get_db),
    current_user: UserInDB = Depends(get_current_user)
):
    db_issue = crud.issue.delete_issue(db, issue_id=issue_id)
    if db_issue is None:
        raise HTTPException(status_code=404, detail="Issue not found")
    return db_issue